<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\AdminUserAssessLogFormat;
use App\Http\Requests\CreateAdminUserAssessLogRequest;
use App\Services\AdminUserService;
use Illuminate\Http\Request;

class AdminUserController extends Controller
{
    protected $adminUserService;

    public function __construct(AdminUserService $adminUserService)
    {
        $this->adminUserService = $adminUserService;
    }

    public function assessAdminUser(CreateAdminUserAssessLogRequest $createAdminUserAssessLogRequest)
    {
        $params = $createAdminUserAssessLogRequest->input('assess');
        foreach ($params as $param) {
            $format = new AdminUserAssessLogFormat($param);
            $this->adminUserService->assessAdminUser($format);
        }
        return renderSuccessJson();
    }

    public function deleteAdminUser(Request $request)
    {
        $adminUserId = $request->input('admin_user_id');
        $userId = $request->input('user_id');
        if ($userId == $adminUserId) {
            throw new ApiException('不能删除自己', 1);
        }
        $this->adminUserService->deleteAdminUser($adminUserId);
        return renderSuccessJson();
    }

    public function getAdminUserAssessLog(Request $request)
    {
        $adminUserId = $request->input('admin_user_id');
        if (empty($adminUserId)) {
            throw new ApiException('缺少参数 admin_user_id', 1);
        }
        $res = $this->adminUserService->getAdminUserAssessLog($adminUserId);
        return renderSuccessJson($res);
    }
}
